package v2

import (
	rpcv1 "gluenet/internal/agent/api/rpc/v1"
	corev1 "gluenet/pkg/agent/types"
	"gluenet/pkg/agent/v2/rpc"
)

type ProxyGetter interface {
	Proxy() GluenetProxyInterface
}

type GluenetProxyInterface interface {
	Get(proxyGet *corev1.ProxyGet) ([]byte, error)
	Post(proxyPost *corev1.ProxyPost) (result []byte, err error)
	Request(proxyRequest *corev1.ProxyRequest) (result []byte, err error)
}

type gluenetProxy struct {
	client rpc.Interface
}

func newGluenetProxy(c *CoreClient) *gluenetProxy {
	return &gluenetProxy{
		client: c.RPCClient(),
	}
}

func (c *gluenetProxy) Get(proxyGet *corev1.ProxyGet) (result []byte, err error) {
	err = c.client.Invoke().
		Api(rpcv1.HttpGetProxy).
		Spec(*proxyGet).
		Do().
		Into(&result)
	return
}

func (c *gluenetProxy) Post(proxyPost *corev1.ProxyPost) (result []byte, err error) {
	err = c.client.Invoke().
		Api(rpcv1.HttpPostProxy).
		Spec(*proxyPost).
		Do().
		Into(&result)
	return
}

func (c *gluenetProxy) Request(proxyRequest *corev1.ProxyRequest) (result []byte, err error) {
	err = c.client.Invoke().
		Api(rpcv1.HttpProxyRequest).
		Spec(*proxyRequest).
		Do().
		Into(&result)
	return
}
